package com.example.nio.tranport.mina.keepAlive;

import com.example.nio.message.Request;
import com.example.nio.message.Response;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/***
 * 心跳工厂
 */
public class MinaKeepAliveMessageFactory implements KeepAliveMessageFactory {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean isRequest(IoSession session, Object message) {
        return message instanceof Request && ((Request) message).isPing();
    }

    @Override
    public boolean isResponse(IoSession session, Object message) {
        return message instanceof Response && ((Response) message).getRequest().isPing();
    }

    @Override
    public Object getRequest(IoSession session) {
        return Request.buildPing();
    }

    @Override
    public Object getResponse(IoSession session, Object request) {
        return new Response((Request) request);
    }
}
